home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: Thu, 29 Feb 96 19:08:46 GMT
- Organization: none
- Message-ID: <825620926snz@genesis.demon.co.uk>
- References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com> <31331a38.54160408@nntp.ix.netcom.com> <1996Feb28.195423.10465@isac.hces.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <1996Feb28.195423.10465@isac.hces.com>
- gg@isac.hces.com "Greg Goodrich" writes:
-
- >Mike Rubenstein (miker3@ix.netcom.com) wrote:
- ...
- >: In many implementations pointer to char and pointer to array of 10
- >: char have the same representation and this will work properly, but
- >: this is not required by the standard.
- >
- >I would like to see an example of how this could be implemented to fail.
-
- While I don't have firsthand knowledge of a practical example it is easy
- to create a theoretical one.
-
- >They both point to the exact same memory address, the only difference
- >being the datatype of the value, which can be type casted if necessary.
-
- And that is the key point. Pointers with different types can have different
- representations, even different sizes - sizeof(int *) may be different
- to sizeof(char *)
-
- >If you have an array, the address of the array is also the address of
- >the first member of the array,
-
- both short and long types can represent the value 1 but on many systems
- they represent it in different ways so a cast between short and long can
- result in a format change (in that case typically adding or removing
- leading bits). The same principle applies to different pointer types -
- 2 pointers may in some sense refer to the same address but may do so using
- different representations.
-
- The FAQ cites examples of this in section 5.17.
-
- >because the array itself is not a
- >pointer, but a reference. The same goes for a structure. The address
- >of a struct is the same as the address of the first member of the
- >struct, but of different data type.
-
- The problem here is that you can't compare pointers to different types
- directly in C so saying they point to the same address is meaningless
- and can't be validated one way or the other. You can of course cast the
- pointers to a common type but you are then potentially changing the
- pointer representations.
-
- > One is pointer to struct and the
- >other is pointer to whatever the first member is declared as, but they
- >are both pointers, and therefore are the same size.
-
- Not in general. The standard says (6.1.2.5)
-
- "A pointer to void shall have the same representation and alignment
- requirements as a pointer to a character type. Similarly, pointers to
- qualified or unqualified versions of compatible types shall have the same
- representation and alignment requirements. Pointers to other types
- need not have the same representation and alignment requirements."
-
-
- char * and char (*)[10] are *not* compatible types.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-